package leetcode;

import java.util.Arrays;

public class jieyushui {
    public static int trap(int[] nums) {
        if(nums==null||nums.length==0){
            return -1;
        }
        int sum=0;
        int max1=0;
        int max2=0;
        int m=0;
        int n=0;
        for(int i=1;i<nums.length-1;i++){
            //则要找到[0,i-1]中的max1与[i+1,nums.length-1]的max2
            //则创建数组，给数组进行排序就可以了
            int[] left=new int[i];
            int[] right=new int[nums.length-1-i];
            for(int j=0;j<i;j++){
                left[j]=nums[j];
            }
            for(int j=0;j<nums.length-i-1;j++){
                right[j]=nums[i+j+1];
            }
            Arrays.sort(left);
            Arrays.sort(right);
            max1=left[i-1];
            //这里发生数组越界
            max2=right[nums.length-i-2];
            m=Math.min(max1,max2);
            n=m-nums[i];
            if(n<=0){
                sum=sum;
            }else{
                sum=sum+n;
            }
        }
        if(nums.length==1||nums.length==2){
            sum=sum;
        }
        return sum;
    }

    public static void main(String[] args) {
        int[]  array={2,1,4,0,2,1,5,2,3,0,1};
        int trap = trap(array);
        System.out.println(trap);
    }
}
